package member;

import java.util.*;

import javax.servlet.http.*;

import model.*;

import org.apache.struts.action.*;
import org.apache.struts.actions.*;

import util.*;

public class MessageAction  extends DispatchAction {
	
	//쪽지 쓰기 폼 불러오기
	public ActionForward getSendForm(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		MessageForm mf = (MessageForm)form;
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		
		//답장일 경우
		if(mf.getReply()==1){
			String receiveID = mf.getSend();
			String sendID = user.getMid();
			mf.setSend(sendID);
			mf.setReceive(receiveID);
			
		//답장이 아닌 경우	
		}else{
//			보내는 사람
			String send = user.getMid();
			//받는 사람
			String receive = request.getParameter("mid");		
			
			mf.setSend(send);
			mf.setReceive(receive);
		}		
		
		ActionForward af = new ActionForward();
		af.setPath("/messagesend.jsp?category=send");
		return af;
		
	}
	
	//쪽지 리스트 불러오기
	public ActionForward receive(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {
	
		
		MessageForm mform = (MessageForm)form;
		String category = request.getParameter("category");		
		
		//유저정보취득
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		String mid = user.getMid();
		
		System.out.println("mid:"+mid);
		
		//받은 메세지를 취득한다.(읽기,보관)
		MessageDAO msg = new MessageDAO();
		int totalRows = 0;
		Pager pager = new Pager();
		
		Vector<MessageForm> list = new Vector<MessageForm>();
		Vector<Message> mList = null;
		
		//받은 쪽지함
		if(category.equals("list"))
		{
			totalRows = msg.getMessageReadCount(mid);
			pager.init(request, totalRows, 10, 5);
			mList = msg.getMessageListPage(mid, pager.getCurrentPage(), pager.getRowsPerPage());
			
			for(int j=0;j<mList.size();j++){
				MessageForm mf = new MessageForm();				
				mf.setMno(mList.get(j).getMno());
				mf.setSend(mList.get(j).getSendId());
				mf.setReceive(mList.get(j).getReceiveId());
				mf.setBehold(mList.get(j).getBehold());
				mf.setCategory("list");
				
				if(mList.get(j).getMessage().length()>10){
					String m = mList.get(j).getMessage().substring(0,9)+"...";
					mf.setMessage(m);
				}else{
					mf.setMessage(mList.get(j).getMessage());
				}
				mf.setSenddate(mList.get(j).getSendDate());
				list.add(mf);
			}			
			mform.setCategory("list");	
		}
		else if(category.equals("save"))	//쪽지보관함
		{
			
			totalRows = msg.getMessageCount(mid , 2);
			pager.init(request, totalRows, 10, 5);
			mList = msg.getMessageListPage(mid, 2 , pager.getCurrentPage(), pager.getRowsPerPage());
			for(int j=0;j<mList.size();j++){
				MessageForm mf = new MessageForm();
				mf.setMno(mList.get(j).getMno());
				mf.setSend(mList.get(j).getSendId());
				mf.setReceive(mList.get(j).getReceiveId());
				mf.setBehold(mList.get(j).getBehold());
				mf.setCategory("save");
				
				if(mList.get(j).getMessage().length()>10){
					String m = mList.get(j).getMessage().substring(0,9)+"...";
					mf.setMessage(m);
				}else{
					mf.setMessage(mList.get(j).getMessage());
				}
				mf.setSenddate(mList.get(j).getSendDate());
				list.add(mf);
			}
			mform.setCategory("save");
			
		}
	
		mform.setList(list);
		
		
		ActionForward af = new ActionForward();
		af.setPath("/messagelist.jsp?section="+category);
		return af;
	}
	
	//쪽지 보내기
	public ActionForward sendMessage(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		MessageForm mf = (MessageForm)form;
		
		Message message = new Message();
		MessageDAO dao = new MessageDAO();		
		
		message.setSendId(mf.getSend());
		message.setReceiveId(mf.getReceive());
		message.setMessage(mf.getMessage());		
		
		dao.sendMessage(message);
		
		
		ActionForward af = new ActionForward();		
		af.setPath("/messageconfirm.jsp");		
		
		return af;			
	}
	
	//쪽지 읽기
	public ActionForward readMessage(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		MessageForm mf = (MessageForm)form;
		
		MessageDAO dao = new MessageDAO();
		int mno = Integer.parseInt(request.getParameter("mno"));		
		
		Message m = dao.getMessage(mno);
		
		mf.setMno(m.getMno());
		mf.setSend(m.getSendId());
		mf.setReceive(m.getReceiveId());
		mf.setMessage(m.getMessage());
		mf.setSenddate(m.getSendDate().substring(0, 19));
		if(m.getBehold()==0){
			dao.updateBehold(mno,1);
		}
		mf.setBehold(m.getBehold());
				
		ActionForward af = new ActionForward();
		af.setPath("/messageview.jsp");
		return af;		
	}
	
	//쪽지 보관
	public ActionForward saveMessage(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		MessageForm mf = (MessageForm)form;		
		MessageDAO dao = new MessageDAO();		
		
		//리스트에서 체크했을 경우
		if(request.getParameter("mno")==null){
			for(int i=0;i<mf.getCheckMno().length;i++){
				dao.updateBehold(mf.getCheckMno()[i], 2);				
			}
		//뷰페이지에서 보관할 경우
		}else{
			int mno = Integer.parseInt(request.getParameter("mno"));
			dao.updateBehold(mno, 2);
		}		
				
		ActionForward af = new ActionForward();
		af.setPath("/message.do?method=receive&category=list");		
		
		return af;
	}
	
	//쪽지 삭제하기
	public ActionForward deleteMessage(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		MessageForm mf = (MessageForm)form;		
		MessageDAO dao = new MessageDAO();
		Message m = new Message();
		
		//리스트에서 체크해서 삭제할 경우
		if(request.getParameter("mno")==null){
			for(int i=0;i<mf.getCheckMno().length;i++){
				dao.delete(mf.getCheckMno()[i]);				
			}			
			
		//뷰페이지에서 삭제할 경우
		}else{
			int mno = Integer.parseInt(request.getParameter("mno"));
			m = dao.getMessage(mno);
			mf.setBehold(m.getBehold());			
			dao.delete(mno);			
		}
		
		ActionForward af = new ActionForward();
		if(mf.getBehold()==2){
			if(mf.getCategory()==null){			
				mf.setCategory("save");
			}
		}else if(mf.getBehold()==1){
			if(mf.getCategory()==null){			
				mf.setCategory("list");
			}
		}		
		
		if(mf.getBehold()==2||mf.getCategory().equals("save")){
			af.setPath("/message.do?method=receive&category=save");
		}else{			
			af.setPath("/message.do?method=receive&category=list");
		}		
		
		return af;
	}
	
}
